<template>
  <!-- 商家列表 -->
  <div class="goodsContent">
    <!-- 左侧类目列表 -->
    <ul class="menu">
      <!-- v-for动态循环生成列表 -->
      <li v-for="(item, index) in goods" :key="item.name">
        <!-- 利用锚点设置数据连动效果 -->
        <a :href="`#${index}`">{{ item.name }}</a>
      </li>
    </ul>
    <!-- 右侧食品列表 -->
    <ol class="list">
      <li v-for="(item, index) in goods" :key="item.name">
        <!-- 菜名 -->
        <h3 :id="index">{{ item.name }}</h3>
        <!-- 菜单信息 -->
        <ul class="info">
          <li v-for="food in item.foods" :key="food.name">
            <div class="box">
              <!-- 图片 -->
              <div class="pic">
                <img :src="food.image" alt="" />
              </div>
              <div class="title">
                <h4>{{ food.name }}</h4>
                <div class="des">{{ food.description }}</div>
                <div class="sell">
                  <span>
                    月售{{ food.sellCount }}份
                  </span>
                  <span class="raing">
                    好评{{ food.rating }}%
                  </span>
                </div>
                <!-- 价格 -->
                <div class="pice">
                  <span class="sp1">￥{{ food.price }}</span>
                  <del class="oldPrice" v-show="food.oldPrice">
                    ￥{{ food.oldPrice }}
                  </del>
                </div>
              </div>
              <!-- 公共组件Btns :item是操作的food对象，type方便判断是否需要定位样式 -->
              <Btns :item="food" type="0"></Btns>
            </div>
          </li>
        </ul>
      </li>
    </ol>

    <!-- 底部购物车 -->
    <div class="shopCar">
      <p class="p1">
        <img :src="imgSrc" alt="" class="iff" @click="showCar">
        ￥{{ getPrice }}
      </p>
      <p class="p2">另需配送费￥4元</p>
      <p class="p3">￥20起送</p>
    </div>
    <!-- 购物车的遮罩层 -->
    <div class="mask" v-show="show">
      <div class="xx">
        <p class="pp">
          <span>购物车</span>
          <span @click="delGoodsCar">清空</span>
        </p>
        <ul>
          <li v-for="(item, index) in carList">
            <p>{{ item.name }}
              <span>￥{{ item.price }}</span>
            </p>
            <Btns :item="item" class="cdd"></Btns>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState ,mapMutations} from "vuex";
import Btns from "./btns.vue";
export default {
  components: {
    Btns,
  },
  created() {
    // 获取商品列表goods数据
    this.getGoods();
  },
  computed: {
    // 利用satate的辅助函数mapState来导出对应的方法，然后使用
    ...mapState(["goods", "price", "carList","show",'img','imgSrc']),
    // 定义函数来计算商品的总价
    getPrice() {
      let num = 0;
      // 循环遍历购物车中的商品用价格乘数量
      this.carList.forEach((item) => {
        num += item.price * item.count;
      });
      return num;
    },
  },
  methods: {
    // 导出方法
    ...mapActions(["getGoods"]),
    ...mapMutations(["delGoodsCar"]),
    // 展示购物车列表
    showCar() {
      this.$store.commit("showCar");
    },
  },
};
</script>

<style lang="less" scoped>
.goodsContent {
  display: flex;
  height: 86%;
  // background-color: brown;
  overflow: hidden;

  ul::-webkit-scrollbar {
    display: none;
  }

  ul {
    overflow: scroll;
  }

  .menu {
    flex: 0 0 82px;
    height: 100%;
    background: #f3f5f7;

    li {
      font-size: 15px;
      // margin: 0px 8px;
      text-align: center;

      span {
        display: block;
        height: 41px;
        font-size: 14px;
        white-space: wrap;
        color: #333;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      }

      a {
        display: inline-block;
        color: rgb(84, 82, 82);
        padding: 20px 0px;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      }

      a:hover {
        color: rgb(0, 0, 0);
        font-weight: 550;
      }
    }

    li:hover {
      background-color: #fff;
    }

    li:nth-child(2)>a::before {
      content: "特";
      display: inline-block;
      color: #fff;
      background-color: rgb(29, 165, 29);
      font-size: 14px;
      border-radius: 10%;
      margin-right: 2px;
      padding: 0px 1px;
    }
  }

  ol::-webkit-scrollbar {
    display: none;
  }

  .list {
    height: 100%;
    flex: 1 1 auto;
    overflow-y: scroll;

    h3 {
      border-left: 4px #d9dde1 solid;
      background: #f3f5f7;
      height: 26px;
      line-height: 26px;
      padding-left: 12px;
      color: rgb(147, 153, 159);
      font-weight: 400;
      font-size: 12px;
    }
    .info li {
      padding: 0 18px;
      position: relative;
      .box {
        display: flex;
        padding: 18px 0;
        border-bottom: 1px rgba(1, 17, 27, 0.1) solid;
        .pic img {
          width: 100px;
          height: 94px;
        }
        .title {
          padding-left: 10px;

          .des {
            font-size: 14px;
            color: rgb(195, 192, 192);
            padding: 5px 0px;
          }

          .sell {
            span {
              display: inline-block;
              font-size: 14px;
              color: rgb(150, 147, 147);
              padding: 4px 0px;
              padding-right: 6px;
            }
          }
          .pice {
            .sp1 {
              font-size: 16px;
              color: red;
            }
            .oldPrice {
              font-size: 12px;
              color: rgb(150, 147, 147);
            }
          }
        }
      }
    }
  }
.shopCar {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    // background: blue;
    // height: 60px;
    z-index: 200;
    color: #fff;
    background-color: #141d27;
    display: flex;
    .p1 {
      font-size: 16px;
      color: rgba(255, 255, 255, 0.4);
      font-weight: 700;
      line-height: 24px;
      border-right: 1px solid rgb(255, 255, 255, 0.1);
      margin: 22px 0px;
      padding-left: 70px;
      padding-right: 12px;
      position: relative;

      .iff {
        position: absolute;
        width: 60px;
        bottom: 10px;
        left: 5px;
      }
    }

    .p2,
    .p3 {
      font-size: 16px;
      color: rgba(255, 255, 255, 0.4);
      // font-weight: 700;
      line-height: 24px;
      padding: 22px 0px;
    }

    .p2 {
      padding-left: 12px;
      flex: 1;

    }

    .p3 {
      text-align: center;
      background-color: rgba(255, 255, 255, 0.4);
      width: 105px;
    }
  }
.mask {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0px;
  background-color: rgba(7, 17, 27, 0.6);
  backdrop-filter: blur(5px);
  .xx {
    width: 100%;
    height: 40%;
    // height: 305px;
    // background-color: aqua;
    padding-bottom: 26px;
    position: absolute;
    bottom: 0;
    background-color: #fff;
    // 滚动条
    overflow-y: scroll;
    .pp{
      display: flex;
      background-color: #f3f5f7;
      // padding: 6px 0px;
      line-height: 40px;
      padding: 0px 18px;
      font-size: 14px;
      justify-content: space-between;
      font-weight: 200;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      span:last-child{
        // background: #141d27;
        color: rgb(0,160,220);
      }
    }
    ul{
      li{
        font-size: 14px;
        color: rgba(7, 17, 27);
        line-height: 48px;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
        margin: 0px 18px;
        height: 48px;
        display: flex;
        font-weight: 700;
        position: relative;
        p{
          span{
            color: red;

          }
        }
        .cdd{
          position: absolute;
          right: 18px;
          top: 10px;
          line-height: 30px;
        }
      }
    }
  }
}
}
</style>
